package com.example.babyapp;

import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Toast;

public class TestActivity extends Activity{
	
	ArrayList<String> testResultRight = new ArrayList<String>();
	ArrayList<String> testResultLeft = new ArrayList<String>();
	
	String strCBResult = "";
	int noTrials = 0;
	MediaPlayer mPlayer = null;
	
	public void onCreate(Bundle bundle){
		super.onCreate(bundle);
        setContentView(R.layout.activity_test);
        setOrientation();
        
        final Button proceedBtn = (Button) findViewById(R.id.proceedBtn);
        proceedBtn.setEnabled(false);
        final Button testBtn = (Button) findViewById(R.id.testBtn);
        
        showRDialog();
    	
    	
        	 final CharSequence[] items = {"The baby cried.", "The baby moved.", "The baby jumped.", "The baby blinked.", "The baby tightened his/her eyelids."};
        	    final boolean[] states = {false, false, false, false, false};
        	    AlertDialog.Builder builder = new AlertDialog.Builder(this);
        	    builder.setTitle("What would you like to do?");
        	    builder.setMultiChoiceItems(items, states, new DialogInterface.OnMultiChoiceClickListener(){
        	        public void onClick(DialogInterface dialogInterface, int item, boolean state) {
        	        }
        	    });
        	    builder.setPositiveButton("Okay", new DialogInterface.OnClickListener(){
        	    	
        	    	String cbResult = "";
        	    	
        	    	public void onClick(DialogInterface dialog, int id){
        	    		
        	        SparseBooleanArray CheCked = ((AlertDialog) dialog).getListView().getCheckedItemPositions();
        	        if (CheCked.get(0)){
        	            cbResult += "The baby cried." + "\n";
        	        	}
        	        if (CheCked.get(1)){
        	            cbResult += "The baby moved." + "\n";
        	        	}
        	        if (CheCked.get(2)){
        	            cbResult += "The baby jumped." + "\n";
        	        	}
        	        if (CheCked.get(3)){
        	            cbResult += "The baby blinked." + "\n";
        	        	}
        	        if (CheCked.get(4)){
        	            cbResult += "The baby tightened his/her eyelids." + "\n";
        	        	}
        	        
        	        if(noTrials < 4){
        	        	testResultRight.add(cbResult);
        	        }else if(noTrials > 3){
        	        	testResultLeft.add(cbResult);
        	        }
        	        
        	        cbResult = "";
        	        
        	        if (noTrials == 3) {
        	    		showLDialog();	
        	    	}
        	        if(noTrials>5){
        	    		proceedBtn.setEnabled(true);
        	    		testBtn.setEnabled(false);
        	    	}
        	        	
        	        	
        	    }		
        	});
        	    
				final AlertDialog alert = builder.create();
        	
        
        
        testBtn.setOnClickListener(new OnClickListener() {
        
	        public void onClick(View v){
	        	noTrials += 1;
	        	mPlayer = MediaPlayer.create(TestActivity.this, R.raw.asd);
	        	mPlayer.start();
	        	alert.show();
	        }
        
        });
	}
	
	public void gotoPreviewActivity(View view){
		Intent intent = new Intent(this, PreviewActivity.class);
		Intent babyIntent = getIntent();
		
		
		String[] babyInfo = babyIntent.getStringArrayExtra("babyInfo");
		String[] secondInfo = babyIntent.getStringArrayExtra("secondInfo");
		String[] otherInfo = babyIntent.getStringArrayExtra("otherInfo");
		
		intent.putExtra("babyInfo", babyInfo);
		intent.putExtra("secondInfo", secondInfo);
		intent.putExtra("otherInfo", otherInfo);
		intent.putExtra("testResultRight", testResultRight);
		intent.putExtra("testResultLeft", testResultLeft);
		
		startActivity(intent);
	}
	
	protected void setOrientation() {
    	int current = getRequestedOrientation();
    	if(current != ActivityInfo.SCREEN_ORIENTATION_PORTRAIT){
    		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    	}
    }
	
	public void showRDialog(){
		
		AlertDialog.Builder alertD = new AlertDialog.Builder(this);
		alertD.setTitle("Instruction");
		alertD.setMessage("Please place the phone 6 inches away from the baby's right ear and make sure that the place is quiet and that the phone's volume is set to maximum before proceeding with the test, then once ready press the start button.");
		
		alertD.setNeutralButton("OK", new DialogInterface. OnClickListener() {

			public void onClick(DialogInterface dialog, int which) {
			}
			
		});
		
		alertD.show();
	}
	public void showLDialog(){
		
		AlertDialog.Builder alertD = new AlertDialog.Builder(this);
		alertD.setTitle("Instruction");
		alertD.setMessage("Please place the phone 6 inches away from the baby's left ear and make sure that the place is quiet and that the phone's volume is set to maximum before proceeding with the test, then once ready press the start button.");
		
		alertD.setNeutralButton("OK", new DialogInterface. OnClickListener() {

			public void onClick(DialogInterface dialog, int which) {
			}
			
		});
		
		alertD.show();
	}

}
